<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请假信息管理系统 -- 添加学生</title>
    <script src="../../../js/jquery.min.js"></script>
    <link href="../../../css/studentManage/addStudent.css" rel="stylesheet">
    <link href="../../../css/common/common.css" rel="stylesheet">
</head>
<body>
<!-- 顶部弹出提示信息容器 -->
<div id="notification" class="notification">
    <span id="message"></span>
</div>
    <!-- 头部 -->
    <header th:insert="~{common/common::header}">
    </header>
    <!-- 左边 -->
    <aside th:insert="~{common/common::aside}">
    </aside>
    <!-- 中间 -->
    <main class="main">
        <form action="/student/addStudent" id="stuForm">
            <h2 style="margin: 10px 35%;">新增学生信息</h2>
            <div class="form-group">
                <label for="stuName">学生姓名</label>
                <input type="text" id="stuName" name="stuName">
            </div>
            <div class="form-group">
                <label for="password">学生密码</label>
                <input type="text" id="password" name="password">
            </div>
            <div class="form-group">
                <label for="selectClass">学生所属班级：
                    <select id="selectClass" style="width: 400px;height: 40px;" name="classID">
                        <option>---请选择---</option>
                    </select>
                </label>
            </div>
            <div class="form-group">
                性别：男<input type="radio" value="男" name="sex" id="radioMan">
                女<input type="radio" value="女" name="sex" id="radioWomen">
            </div>
            <div class="form-group">
                <label for="address">地址</label>
                <input type="text" id="address" name="address">
            </div>
            <div class="form-group">
                <label for="stuTel">学生电话</label>
                <input type="text" id="stuTel" name="stuTel">
            </div>
            <div class="form-group">
                <label for="contact">联系人</label>
                <input type="text" id="contact" name="contact">
            </div>
            <div class="form-group">
                <label for="contactTel">联系人电话</label>
                <input type="text" id="contactTel" name="contactTel">
            </div>
            <input type="button" value="返回" onclick="goBack()">
            <input type="button" value="保存提交" onclick="addStudent()">
        </form>

    </main>
</body>

<script th:inline="javascript" type="text/javascript">
    function goBack(){
        window.location.href = '/student/list?pageSize=7&pageNum=1';
    }
    var classList = [[${classes}]];
    var className = []
    classList.forEach(item => {
        className.push({
            "label":item.className,
            "value":item.classID
        })
    })
    var selectElement = document.getElementById("selectClass");
    className.forEach(option =>{
        var optionElement = document.createElement("option");
        optionElement.value = option.value;
        optionElement.textContent = option.label;
        selectElement.appendChild(optionElement);
    })

    function validatePhoneNumber(phoneNumber) {
        // 中国的电话号码正则表达式
        const phoneRegex = /^1[3-9]\d{9}$/;

        if (phoneRegex.test(phoneNumber)) {
            return true;
        } else {
            return false;
        }
    }

    function addStudent(){
        var stuName = document.getElementById("stuName").value;
        var password = document.getElementById("password").value;
        var address = document.getElementById("address").value;
        var stuTel = document.getElementById("stuTel").value;
        var contact = document.getElementById("contact").value;
        var contactTel = document.getElementById("contactTel").value;
        if(stuName.length > 50){
            document.getElementById("message").innerHTML = "学生姓名过长，请重新输入";
            this.showNotification();
            return false;
        }
        if(password.length == 0 || password.length > 50){
            document.getElementById("message").innerHTML = "密码过长，请重新输入";
            this.showNotification();
            return false;
        }
        if(address.length > 150){
            document.getElementById("message").innerHTML = "地址过长，请重新输入";
            this.showNotification();
            return false;
        }
        if(!validatePhoneNumber(stuTel)){
            document.getElementById("message").innerHTML = "电话格式不正确，请重新输入";
            this.showNotification();
            return false;
        }
        if(contact.length > 30){
            document.getElementById("message").innerHTML = "联系人过长，请重新输入";
            this.showNotification();
            return false;
        }
        if(!validatePhoneNumber(contactTel)){
            document.getElementById("message").innerHTML = "联系人电话格式不正确，请重新输入";
            this.showNotification();
            return false;
        }
        document.getElementById("stuForm").submit();
    }



    function showNotification() {
        const notification = document.getElementById("notification");
        notification.classList.add("show");

        setTimeout(function() {
            notification.classList.remove("show");
        }, 2000); // 3秒后自动消失
    }

    function outLogin(){
        location.href = '/outLogin'
    }
</script>

</html>